package playn.game.core.transitions;

import java.util.ArrayList;

import playn.core.Layer;
import playn.game.core.Transition;

public class Fade implements Transition {
	private float counter=0;

	private final static float FULL_TRANSITION_TIME=300;

	private ArrayList<Layer> layers_in=new ArrayList<Layer>();
	private ArrayList<Layer> layers_out=new ArrayList<Layer>();
	
	public void addIn(Layer layer){
		layers_in.add(layer);
	}
	public void addOut(Layer layer){
		layers_out.add(layer);
	}
	
	public boolean in(float delta) {
		counter+=delta;
		counter=Math.min(FULL_TRANSITION_TIME, counter);

		setIn(counter/FULL_TRANSITION_TIME);

		if(counter==FULL_TRANSITION_TIME){
			counter=0;
			return true;
		}

		
		return false;
	}

	public boolean out(float delta) {
		counter+=delta;
		counter=Math.min(FULL_TRANSITION_TIME, counter);

		setOut(1-counter/FULL_TRANSITION_TIME);

		if(counter==FULL_TRANSITION_TIME){
			counter=0;
			return true;
		}
		
		return false;
	}
	
	private void setIn(float alpha){
		for(Layer layer:layers_in){
			
			float a=layer.alpha();
			if(alpha>a){
				layer.setAlpha(alpha);
			}
			System.out.println("in="+alpha);
		}
	}

	private void setOut(float alpha){
		for(Layer layer:layers_out){
			layer.setAlpha(alpha);
			System.out.println("out="+alpha);
		}
	}

}
